import React from 'react';
import Paho from 'paho-mqtt'
import Util from '../utils/utils'
import {message} from 'antd';

const mqttObject = {
    hostname : Util.getMqttUrl(),
    port: Number(8083),
    clientId: '',//唯一的client id 字符串，当连接中间人时使用。如果client_id 长度为0，则会自动随机产生一个client_id。这种情况下 clean_session 参数必须为 True
    timeout: 5,
    cleanSession: true,//如果为True，当client 失去连接时中间人将删除所有关于这个client 的信息。 如果为 False，则说明这是一个持久的client，当失去连接时发布信息和队列中的消息将会被保持。注意，当失去连接时client 永远不会抛弃自己需要发出的信息，当调用connect()/reconnect() 后将会重新发送这些信息。 调用 reinitialise() 重置client到它初始化时的状态
    keepAlive: 100,
    // userName : 'mao2080',
    // password : '123'
    ssl: false
};
var client = {};
export default {
    createClient(onConnect,onConnectionLost,onMessageArrived,onFailure) {
        client = new Paho.Client(mqttObject.hostname, mqttObject.port, mqttObject.clientId);
        var options = {
            invocationContext: {
                host: mqttObject.hostname,
                port: mqttObject.port,
                path: client.path,
                clientId: mqttObject.clientId
            },
            timeout: mqttObject.timeout,
            keepAliveInterval: mqttObject.keepAlive,
            cleanSession: mqttObject.cleanSession,
            useSSL: mqttObject.ssl,
            // userName: userName,
            // password: password,
            onSuccess:onConnect,
            onFailure:onFailure,
        };
        client.connect(options);
        client.onConnectionLost = onConnectionLost;
        client.onMessageArrived = onMessageArrived;
        return client;
    },
}